/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import CRUD.MemberHome;
import CRUD.ThreadHome;
import MemberOperation.MemberThreadOperation;
import beans.MarkLurk;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import xmlMapping.Forum;
import xmlMapping.Member;
import xmlMapping.Thread;

/**
 *
 * @author Amira
 */
public class MarkLurkAction extends DispatchAction {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction1,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    public ActionForward setThreadLurk(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
         
        String threadid =request.getParameter("threadid");
        int thid=Integer.parseInt(threadid);

        
        MarkLurk marklurk = new MarkLurk();
      
        Integer memberID =(Integer) request.getSession(false).getAttribute("id");
       ThreadHome th=new ThreadHome();
        Thread mythread=th.findById(thid);
        String  fname=mythread.getForum().getName();
        Integer fid=mythread.getForum().getForumId();
        th.session.close();
        marklurk.setMemberId(memberID);
        marklurk.setId(thid);
        MemberThreadOperation.setThreadLurk(marklurk);

        response.sendRedirect("ViewForum.jsp?forumname"+fname+"&forumid="+fid);
   
        return null;
    }

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction2,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    public ActionForward setThreadMark(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
       // Member member = new MemberHome().findById((Integer) request.getSession().getAttribute("id"));
        String threadid =request.getParameter("threadid");
        
        int threadID = Integer.parseInt(threadid);
        ThreadHome th=new ThreadHome();
        Thread mythread =th.findById(threadID);
         
        String  fname=mythread.getForum().getName();
        Integer fid=mythread.getForum().getForumId();
        
        th.session.close();
        
        MarkLurk marklurk = new MarkLurk();
        int memberID = (Integer) request.getSession().getAttribute("id");
       
        marklurk.setMemberId(memberID);
        marklurk.setId(threadID);
         MemberThreadOperation threadoperation=new MemberThreadOperation();
         threadoperation.setThreadMark(marklurk);

        response.sendRedirect("ViewForum.jsp?forumname"+fname+"&forumid="+fid);
   
        return null;
    }

    public ActionForward removeThreadMark(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
      //  Member member = new MemberHome().findById();
        String threadid =request.getParameter("threadid");
        int thid=Integer.parseInt(threadid);
        ThreadHome th=new  ThreadHome();
        Thread thread = th.findById((thid));
       
        String  fname=thread.getForum().getName();
        Integer fid=thread.getForum().getForumId();
         th.session.close();
        MarkLurk marklurk = new MarkLurk();
        int memberID = (Integer) request.getSession().getAttribute("id");
        int threadID = thread.getThreadId();
        marklurk.setMemberId(memberID);
        marklurk.setId(thid);
        MemberThreadOperation memberoperation=new MemberThreadOperation();
        memberoperation.removeThreadMark(marklurk);

        response.sendRedirect("ViewForum.jsp?forumname"+fname+"&forumid="+fid);
   
        return null;
    }

    public ActionForward removeLurk(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
      //  Member member = new MemberHome().findById();
        String threadid =request.getParameter("threadid");
        int thid=Integer.parseInt(threadid);
        ThreadHome th=new ThreadHome();
        Thread thread = th.findById((thid));
         String  fname=thread.getForum().getName();
        Integer fid=thread.getForum().getForumId();
        th.session.close();
        MarkLurk marklurk = new MarkLurk();
        int memberID = (Integer) request.getSession().getAttribute("id");
        
        marklurk.setMemberId(memberID);
        marklurk.setId(thid);
        MemberThreadOperation.removeLurk(marklurk);

        response.sendRedirect("ViewForum.jsp?forumname"+fname+"&forumid="+fid);
   
        return null;
    }
}
